<template>
  <div class="annotationWrapper">
    <el-form
      :inline="true"
      ref="headForm"
      :model="headFormData"
      class="search-form"
      style="display: flex; justify-content: space-between"
    >
      <div>
        <el-form-item label="文件名称:" class="form-input">
          <el-input v-model="headFormData.fileName"></el-input>
        </el-form-item>
        <el-form-item label="抽取模型:" class="form-input">
          <el-select
            v-model="headFormData.extractionModelId"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.modelName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态:" class="status form-input">
          <el-select v-model="headFormData.status" clearable>
            <el-option label="全部" :value="null"></el-option>
            <el-option label="待确认" :value="0"></el-option>
            <el-option label="已确认" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit(1)" size="small">
            查询</el-button
          >
        </el-form-item>
      </div>
      <el-form-item style="text-align: right">
        <el-button size="small" @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>

    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">抽取数据</span>
        <div></div>
      </div>
      <el-table
        :data="tableList"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
        class="km-table"
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="fileName"
          label="文件名"
          align="center"
          show-overflow-tooltip
          min-width="300"
        ></el-table-column>
        <el-table-column
          prop="extractionModelName"
          label="抽取模型"
          align="center"
          show-overflow-tooltip
          width="150"
        ></el-table-column>
        <el-table-column
          prop="entityNum"
          label="已抽取实体数量"
          align="center"
          show-overflow-tooltip
          width="150"
        ></el-table-column>
        <el-table-column
          prop="propertyNum"
          label="已抽取属性数量"
          align="center"
          show-overflow-tooltip
          width="150"
        >
        </el-table-column>
        <el-table-column
          prop="relationNum"
          label="已抽取关系数量"
          align="center"
          show-overflow-tooltip
          width="150"
        >
        </el-table-column>
        <el-table-column
          prop="confirmStatus"
          label="状态"
          align="center"
          show-overflow-tooltip
          width="100"
        >
          <template slot-scope="scope">
            <template v-if="scope.row.confirmStatus === 0">
              <i class="little-circle-info"></i>
              待确认
            </template>
            <template v-else-if="scope.row.confirmStatus === 1">
              <i class="little-circle-primary"></i>
              已确认
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="linkToCheckPage(scope.row)"
              >查看
            </el-link>
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="linkToEditPage(scope.row)"
              v-if="
                scope.row.confirmStatus === 0 &&
                [1, 2].includes(scope.row.extractionType)
              "
              >校核</el-link
            >
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="deleteData(scope.row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="headFormData.pageSize"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="headFormData.pageIndex"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "index",
  components: {},
  data: () => {
    return {
      headFormData: {
        fileName: null,
        extractionModelId: null,
        pageIndex: 1, // 页码
        pageSize: 10, // 每页条数
        status: null, // 状态 null 全部 0 待确认 1 已确认
        parentId: null,
      },
      options: [],
      tableList: [],
      tableSelection: [],
      tableLoading: false,
      total: 0,
    };
  },
  created() {
    this.headFormData.parentId = this.$route.query.id;
    this.getExtractData();
    this.getOptions();
    this.$eventBus.$once("knowledgeGeneration_extractFinish", () =>
      this.getExtractData()
    );
  },

  methods: {
    handleSelectionChange(e) {
      this.tableSelection = e.map((e) => e.id);
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.headFormData.pageIndex = page;
      this.getExtractData();
    },
    // 修改每页条数
    handleSizeChange() {
      this.getSourceDataList(this.filterData);
    },

    // 获取标注数据
    async getExtractData() {
      this.tableLoading = true;
      let res = await this.$api.knowledgeGeneration.getExtractTaskSubList(
        this.headFormData
      );
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.tableList = res.data.data.list;
      this.total = res.data.data.total;
      this.tableLoading = false;
    },

    // 获取本体模型下拉框数据
    async getOptions() {
      let res = await this.$api.knowledgeGeneration.getExtractModel();
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.options = res.data.data;
    },

    onSubmit(pageIndex) {
      this.headFormData.pageIndex = pageIndex
        ? pageIndex
        : this.headFormData.pageIndex;
      this.getExtractData();
    },

    // 跳转查看页
    linkToCheckPage(row) {
      this.$router.push({
        path: `/knowledgeGeneration/extract/detail/${row.id}`,
        query: {
          type: row.extractionType,
        },
      });
    },

    // 跳转校核页
    linkToEditPage(row) {
      console.log(
        "%c [  ]-261",
        "font-size:13px; background:pink; color:#bf2c9f;",
        row
      );
      let fileName = row.fileName || "";
      let fileType = "";
      if (fileName) {
        fileType = fileName.split(".").pop().toLowerCase();
      }
      if (["png", "jpg", "jpeg"].includes(fileType)) {
        if (
          row.entityNum === 0 &&
          row.propertyNum === 0 &&
          row.relationNum === 0
        ) {
          this.$message.error("未抽取有效数据，不能进行校核");
          return;
        }
        this.$router.push({
          path: "/knowledgeGeneration/extract/proofreadPic",
          query: {
            id: row.id,
          },
        });
      } else {
        if (
          row.entityNum === 0 &&
          row.propertyNum === 0 &&
          row.relationNum === 0
        ) {
          this.$message.error("未抽取有效数据，不能进行校核");
          return;
        }
        this.$router.push({
          path: "/knowledgeGeneration/extract/proofread",
          query: {
            id: row.id,
          },
        });
      }
    },

    // 删除
    deleteData(id) {
      console.log("id", id);
      this.$confirm(
        "是否确认删除所选数据？删除后数据不可恢复，请谨慎操作。",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          // 删除接口
          await this.deleteExtractData(id);
          this.getExtractData();
        })
        .catch(() => {});
    },

    async deleteExtractData(id) {
      let res = await this.$api.knowledgeGeneration.deleteExtractionRecord({
        recordId: id,
      });
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.$message.success("操作成功!");
    },
  },
};
</script>
<style lang="scss" scoped>
.annotationWrapper {
  .search-form {
    background: #ffffff;
    border-radius: 4px;
    padding: 16px 32px;
    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;
      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-radius: 4px;

    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      &-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }

    .ontology-edit {
      padding: 0 10px;
      & + .ontology-edit {
        border-left: 1px solid #dddddd;
      }
    }
  }

  .dialog-inline {
    /deep/ .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
}
.light {
  &::before {
    content: "";
    display: inline-table;
    margin-right: 6px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    position: relative;
    top: -2px;
  }
  &-red::before {
    background: #ee5564;
  }
  &-green::before {
    background: #55c898;
  }
  &-yellow::before {
    background: #ffaa45;
  }
}
</style>
<style lang="scss">
.el-message.el-message-loading {
  .el-message__content {
    .el-loading-spinner {
      margin-top: -17px;
      .circular {
        width: 36px;
        height: 36px;
      }
      text-align: left;
    }
    .el-message-loading__text {
      margin-left: 40px;
    }
  }
}
</style>
